
//查看调货列表时需要的数据
var WATCH_DATA = {

    page : 1,

    per_page : 10,

    sort_by : '',

    order : ''

};

//vue的data
var _Global_Data = {

    "provider_status": {

        "0": "客户自有商品",

        "1": "未申请调货",

        "5": "调货申请中",

        "10": "已支付",

        "20": "已发货",

        "30": "已收货"

    },                      //调货处理状态映射表

    transfer_query_info : WATCH_DATA,      //调货查询所需要的数据

    transfer_search_info : {},       //用于接受调货查询数据

    transfer_info : {},          //调货信息的订单信息

    transfer_detail : {},       //调货单详情

    current_id : '',            //当前的id

    remark : '',                 //发货的备注

    normal_sort : {

        paid_at : false,

        created_at : false,

        provider_status : false

    }                             //正序

};

// 调货
var TransferResource = new Resource( API_LIST.TRANSFER_LIST , Vue.http );

!function transferCreate() {

    var vm = new Vue({

        el : '#transfer_handle',

        data : _Global_Data,

        created : function(){

            this.transferList();

        },

        computed : {


        },

        methods : {
            //排序
            sortFun : function sortFun(e){

                this.transfer_query_info.sort_by = e.target.dataset.name;

                if( this.normal_sort[e.target.dataset.name] ){

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.transfer_query_info.order = 'desc';

                }else {

                    this.normal_sort[e.target.dataset.name] = !this.normal_sort[e.target.dataset.name];

                    this.transfer_query_info.order = 'asc';

                }

                this.transfer_search_info = JSON.parse(JSON.stringify(this.transfer_query_info));

                this.transferList(this.transfer_query_info);

            },
            //调货列表改变页数的时候出发的函数
            transferPageChange : function transferPageChange(val){

                this.transfer_search_info.page = val;

                this.transfer_search_info.per_page = this.transfer_query_info.per_page;

                this.transferList(this.transfer_search_info);

            },
            //调货列表改变每页显示数时触发的函数
            transferSizeChange : function transferSizeChange(val){

                this.transfer_search_info.per_page = val;

                this.transfer_query_info.per_page = val;

                this.transferList(this.transfer_search_info);

            },
            //改变订单状态的方法
            orderStatusChange : function orderStatusChange(data){

                var self = this;

                App.blockUI('#order_list_form');

                var param = {

                    process_status : data.process_status

                };

                orderResource.update( data.id, param )
                            .then(function(res){

                                if( parseInt(res.data.code) === 0 ){

                                    self.requestTableData(self.search_info);

                                    alert('修改成功！');

                                }else {

                                    alert(res.data.error.process_status[0]);

                                }

                                App.unblockUI('#order_list_form');

                            })
                            .catch(function(err){

                                console.log(err);

                                App.unblockUI('#order_list_form');

                                alert('网络异常，请重试！');

                            })

            },
            //查看调货详情
            transferDetail : function transferDetail(id){

                var self = this;

                App.blockUI('#detail_form');

                TransferResource.show(id)
                    .then(function (res) {

                        if (parseInt(res.data.code) === 0) {

                            self.transfer_detail = res.data.transfer_cargo_list;

                        } else {

                            alert(res.data.error);

                        }

                        App.unblockUI('#detail_form');

                    })
                    .catch(function (err) {

                        console.log(err);

                        App.unblockUI('#detail_form');

                        alert('网络异常！');

                    })
            },
            //点击发货之后触发的方法
            getCurrentId : function getCurrentId(id){

                this.current_id = id;

            },
            //发货
            confirmDeliver : function confirmDeliver(){

                var self = this;

                App.blockUI('#express_form');

                var post_data = {
                    remark : this.remark
                };

                TransferResource.update( this.current_id , post_data )
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        self.transferList();

                                        alert('发货成功！');

                                        $('.close').trigger('click');

                                    } else {

                                        throw new Error(res.data.error);

                                    }

                                    App.unblockUI('#express_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    alert(err);

                                    App.unblockUI('#express_form');

                                })
                
            },
            //获取所有调货列表
            transferList : function transferList(params){
                
                var self = this;

                App.blockUI('#list_form');

                TransferResource.index(params)
                                .then(function (res) {

                                    if (parseInt(res.data.code) === 0) {

                                        self.transfer_info = res.data.transfer_cargoes;

                                        self.transfer_info.total = parseInt(self.transfer_info.total);

                                        self.transfer_info.per_page = parseInt(self.transfer_info.per_page);

                                        self.transfer_info.current_page = parseInt(self.transfer_info.current_page);

                                    } else {

                                        throw new Error(res.data.error);

                                    }

                                    App.unblockUI('#list_form');

                                })
                                .catch(function (err) {

                                    console.log(err);

                                    App.unblockUI('#list_form');

                                    alert(err);

                                })
                
            }
        }

    })

}();